package ru.yandex.api.money.model;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author: Melnikov Ivan melnikov.ivan@gmail.com
 */
public class History {
	
	private List<Operation> operations = new ArrayList<Operation>();
	private final int nextRepord;
	private final String error;
	
	public History(JSONObject json) {
		try {
			JSONArray ops = json.getJSONArray("operations");
			for (int i=0; i<ops.length(); i++) {
				JSONObject op = ops.getJSONObject(i);
				operations.add(new Operation(op));
			}
			nextRepord = json.getInt("next_record");
			error = json.getString("error");
		} catch (JSONException e) {
			throw new RuntimeException("Parsing failed", e);
		}
	}

	public List<Operation> getOperations() {
		return operations;
	}

	public int getNextRepord() {
		return nextRepord;
	}

	public String getError() {
		return error;
	}

	@Override
	public String toString() {
		return operations.size()+" operations";
	}

}
